<template>
  <div class="body_content">
    <div class="login_panel">
      <div class="title">
        <img src="../../assets/logo.png">
        <span>数字化施工管理平台</span>
        <span class="tips">用户登录</span>
      </div>
      <div>
        <el-form :model="loginInfo" class="demo-form-inline">
          <el-form-item>
            <span class="input_box">
              <i class="fa fa-user"></i>
              <input v-model="loginInfo.user" v-keyup placeholder="请在此输入用户名"></input>
            </span>
          </el-form-item>
          <el-form-item>
            <span class="input_box">
              <i class="fa fa-lock"></i>
              <input v-model="loginInfo.password" v-keyup type="password" placeholder="请在此输入密码"></input>
            </span>
          </el-form-item>
          <div class="checkbox_panel">
            <el-checkbox label="记住密码" v-model="isChecked"></el-checkbox>
            <input type="button" id="code" v-keyup @click="createCode" class="verification" v-model="checkCode" />
          </div>
          <el-form-item>
            <span class="input_box">
              <input class="confirm_input" v-keyup v-model="loginInfo.confirm" placeholder="请在此输入验证码"></input>
            </span>
          </el-form-item>
        </el-form>
        <div class="login_operate">
          <el-button type="warning" class="btn_submit" @click="login">登录</el-button>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
  import {
    login
  } from "../../service/service_zbw";
  import {
    mapState,
    mapActions,
    mapMutations
  } from 'vuex';
  export default {
    directives: {
      keyup: {
        // 指令的定义
        bind: (el, binding, vnode) => {
          el.addEventListener('keyup', ev => {
            if (ev.keyCode === 13) {
              vnode.context.login();
            }
          })
        }
      }
    },
    data() {
      return {
        loginInfo: {
          user: '',
          password: '',
          confirm: ''
        },
        checkCode: '',
        isChecked: false
      }
    },
    computed: {
      ...mapState({
        userDept: state => state.userDept
      }),
    },
    methods: {
      ...mapActions([
        'GetUserDept',
        'GetUserRealDept',
        'GetRoleList',
        //'GetRoleTree',
        'GetRoleIdList',
        'GetRolePowerList',
        'GetUserPowers'
      ]),
      init() {
        this.cookieOperate();
        this.createCode();
      },
      //登陆
      async login() {
        //校验验证码是否正确
        if (this.loginInfo.confirm.toUpperCase() !== this.checkCode) {
          this.$message({
            message: '验证码不正确，请重新输入',
            type: "error"
          });
          this.createCode();
          return;
        }
        //判断环境是否支持 crypto 模块
        // let crypto;
        // try {
        //   crypto = require('crypto');
        // } catch (err) {
        //   console.log('不支持 crypto!');
        // }
        // //md5加密
        // let hashPass = crypto.createHmac('md5', this.loginInfo.password)
        //   .digest('hex');
        let param = {
          userName: this.loginInfo.user,
          passwd: this.loginInfo.password,
        };
        let res = await login.login(param);
        if (res.success) {
          if (res.obj === 0) {
            this.$message({
              message: res.msg,
              type: 'warning'
            });
            return;
          }
          sessionStorage.setItem('USERTOKEN', res.obj);
          //判断是否要记住密码
          if (this.isChecked) {
            localStorage.setItem("USER", this.loginInfo.user);
            localStorage.setItem("PASS", this.loginInfo.password);
          } else {
            localStorage.removeItem("USER");
            localStorage.removeItem("PASS");
          }
          this.getBaseInfo();
          this.$router.push('/index');
        } else {
          this.$message({
            message: res.msg,
            type: 'error'
          });
        }
      },
      /**
       * 获取用户基本信息
       */
      getBaseInfo() {
        //根据用户ID获取部门
        this.GetUserDept();
        this.GetUserRealDept();
        //获取角色列表
        this.GetRoleList();
        this.GetRoleIdList();
        //获取角色树形列表
        //this.GetRoleTree();
        //获取角色可选权限列表
        this.GetRolePowerList();
        //获取当前用户的权限
        this.GetUserPowers();
      },
      /**
       * 根据用户ID获取部门
       */
      getDeptByUserId() {
        this.GetUserDept();
      },
      //判断当前有没有记住的密码
      cookieOperate() {
        if (localStorage) {
          if (localStorage.getItem("USER") != null && localStorage.getItem("PASS") != null) {
            this.loginInfo.user = localStorage.getItem("USER");
            this.loginInfo.password = localStorage.getItem("PASS");
            this.isChecked = true;
          } else {
            this.isChecked = false;
          }
        } else {
          this.isChecked = false;
        }
      },
      // 图片验证码
      createCode() {
        let code = "";
        let codeLength = 4; //验证码的长度
        let random = new Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L',
          'M', 'N', 'O', 'P', 'Q', 'R',
          'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'); //随机数
        for (let i = 0; i < codeLength; i++) { //循环操作
          let index = Math.floor(Math.random() * 36); //取得随机数的索引（0~35）
          code += random[index]; //根据索引取得随机数加到code上
        }
        this.checkCode = code; //把code值赋给验证码
      },
    },
    mounted() {
      this.init();
    }
  }

</script>

<style scoped>
  .body_content {
    width: 100%;
    height: 100%;
    background: url('../../assets/login_bg.png') no-repeat;
    background-size: cover;
  }

  .login_panel {
    position: absolute;
    top: 50%;
    left: 50%;
    margin-top: -190px;
    margin-left: -190px;
    width: 380px;
    height: 390px;
    background: rgba(62, 66, 74, .8);
  }

  .login_panel .title {
    margin: 30px 30px;
    margin-bottom: 36px;
  }

  .login_panel .title img {
    margin-top: -6px;
    margin-right: 16px;
  }

  .login_panel .title span {
    font-size: 20px;
    color: #FFF;
  }

  .login_panel .title .tips {
    float: right;
    margin-top: 3px;
    font-size: 18px;
    color: #b2b2b2;
  }

  .input_box {
    display: block;
    width: 320px;
    height: 40px;
    margin: 0 auto!important;
    line-height: 40px;
    border: 1px solid #c1c1c1;
    border-radius: 4px;
  }

  .input_box i {
    margin-left: 14px;
    margin-right: 12px;
    font-size: 16px;
    color: #c1c1c1;
  }

  .input_box input {
    width: 276px;
    height: 30px;
    background: transparent;
    caret-color: #c1c1c1;
    color: #c1c1c1;
  }

   ::-webkit-input-placeholder {
    color: #999;
    font-size: 14px;
    font-style: italic;
  }

   :-moz-placeholder {
    color: #999;
    font-size: 14px;
    font-style: italic;
  }

   :-ms-input-placeholder {
    color: #999;
    font-size: 14px;
    font-style: italic;
  }

  .checkbox_panel {
    margin-left: 42px;
    margin-right: 58px;
    margin-bottom: 18px;
  }

  .confirm_input {
    margin-left: 38px;
  }

  .login_operate {
    width: 320px;
    margin: 0 auto;
  }

  .btn_submit {
    width: 100%;
    background: #ffa24a!important;
    color: #fff;
    font-size: 14px;
    font-weight: bold;
  }

  .verification {
    vertical-align: middle;
    transform: translate(-15px, 0);
    width: 102px;
    float: right;
  }

</style>

